]]
Conditional evaluation within shell scripts
TLDR
View documentation for the [[ keyword
SYNOPSIS
[[ expression ]]
PARAMETERS
-n string
True if string is non-empty
-z string
True if string is empty
string1 == string2
True if strings equal (or =~ for regex)
string1 =~ regex
True if string matches regex
file -ef file2
True if files have same device/inode
num1 -eq num2
Arithmetic equality (also -ne, -lt, etc.)
expr1 && expr2
Logical AND
expr1 || expr2
Logical OR
DESCRIPTION
The "]]" is not a standalone Linux command but the required closing delimiter for the [[ extended conditional expression construct in Bash (and compatible shells like Zsh). Introduced as a Bash keyword, [[ ... ]] provides advanced string and arithmetic comparisons superior to the traditional test or [ ... ] command.
It supports pattern matching, regex, file tests, and logical operators without needing escapes for most characters. Usage errors, like missing ]], cause syntax errors during script parsing. For example:if [[ $var == "foo*" ]]; then echo "Matches"; fi
This construct enhances script readability and power, avoiding test's limitations like unescaped < or >. It's non-forking (built-in), faster for conditionals in loops or functions. Common in modern shell scripting for robust testing.
CAVEATS
]] must immediately follow the expression without spaces before it; unpaired [[ causes "syntax error near unexpected token". Not portable to POSIX sh (use [ instead). Variables inside need quoting to avoid word-splitting.
EXAMPLES
[[ -d /tmp ]] && echo "Dir exists"
[[ $UID -eq 0 ]] && echo "Root user"
[[ "$1" =~ ^[0-9]+$ ]] || { echo "Not a number"; exit 1; }
EXIT STATUS
Like test: 0 for true, 1 for false, 2+ for errors.
HISTORY
Introduced in Bash 2.02 (1998) to extend POSIX test. Evolved with regex support in Bash 3.0 (2005). Widely adopted for its convenience over external test binary.


